package net.domor.pms.sys.web;

import net.domor.pms.common.web.BaseController;
import net.domor.pms.sys.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@Controller
public class LoginController extends BaseController {

    @RequestMapping(value = "${adminPath}/login", method = RequestMethod.GET)
    public String loginGet(HttpServletRequest request, HttpServletResponse response) {
        /*
        1. 获取 Principal
        2. 如果已登录，则跳转到首页
        3. 如果没有登录，则跳转到登录页面（目前直接跳到登录页）
         */
        return "sys/login";
    }

    @RequestMapping(value = "${adminPath}/login", method = RequestMethod.POST)
    public String loginPost(HttpServletRequest request, HttpServletResponse response, @RequestBody User user) {
        System.out.println("======================================================");
        System.out.println(user);
        return "sys/index";
    }

    /**
     * 如果用户已经登录，则跳转到首页；如果用户没有登录，则跳转到登录页面。
     * 目前还没有引用安全框架，所以此处只是重定向到登录
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "${adminPath}")
    public String index(HttpServletRequest request, HttpServletResponse response) {
        return new StringBuffer("redirect:").append(adminPath).append("/login").toString();
    }

    @ResponseBody
    @RequestMapping("/json")
    public Object json() {
        Map<String, String> map = new HashMap<String, String>();
        map.put("loginName", "WangGuocheng");
        map.put("password", "123456");
        return map;
    }
}
